<?php
namespace app\admin\controller;
use think\Controller;
use Util\Sysdb;

class Account extends Controller
{
    public function login()
    {
        return $this->fetch();
    }

    //管理员登录
    public function dologin(){
        header("Content-type: text/html; charset=utf-8");
        $phone = trim(input('post.phone'));
        $pwd = trim(input('post.pwd'));
        $verifycode = trim(input('post.verifycode'));
        //写日志
        $ip = array_key_exists('HTTP_X_FORWARDED_FOR',$_SERVER)?$_SERVER['HTTP_X_FORWARDED_FOR']:$_SERVER['REMOTE_ADDR'];
        $text = date("Y-m-d H:i:s").'   登录账户'.$phone.'   密码'.$pwd.'   ip'.$ip;
        $logFileDir = DOCUMENT_ROOT.'/Log';
        if (!is_dir($logFileDir)){
            mkdir($logFileDir,0777,true);
        }
        $path = $logFileDir.'/login';
        if (!is_dir($path)){
            mkdir($path,0777,true);
        }
        $file = $path.'/login_'.date('Y_m_d').'.txt';
        file_put_contents($file,$text.PHP_EOL,FILE_APPEND);  // 最简单的快速的以追加的方式写入写入方法，
        if($phone == ''){
            exit(json_encode(array('code'=>1,'msg'=>'手机号不能为空')));//将数组转换成json字符串{"code":1,"msg":'用户名不能为空'}
        }
        if($pwd == ''){
            exit(json_encode(array('code'=>1,'msg'=>'密码不能为空')));
        }
//        if($verifycode == ''){
//            exit(json_encode(array('code'=>5,'msg'=>'请输入验证码')));
//        }
        //验证验证码
//        if(!captcha_check($verifycode)){
//            exit(json_encode(array('code'=>'1','msg'=>'验证码错误')));
//        }
        //验证用户
        $this->db = new Sysdb;//实例化封装的数据库类
        $admin = $this->db->table('admin_admins')->where(array('phone'=>$phone))->item();
        if(!$admin){//判断查询的数据是否为空
            exit(json_encode(array('code'=>1,'msg'=>'用户不存在')));
        }
        if(md5($admin['phone'].$pwd)!= $admin['password']){
            exit(json_encode(array('code'=>1,'msg'=>'密码错误')));
        }
        if($admin['status'] == 1){
            exit(json_encode(array('code'=>1,'msg'=>'账户已被封禁')));
        }elseif($admin['status']==2){
            exit(json_encode(array('code'=>1,'msg'=>'账户审核中，请耐心等待')));
        }
        //设置用户session
        session('admin',$admin);//将查询到的数据库信息存储在session中
        exit(json_encode(array('code'=>0,'msg'=>'登录成功')));
    }

    public function signin()
    {
        if(!$_POST){
            $this->db = new Sysdb;
            $title = $this->db->table('admin_groups')->lists();
            $this->assign('title',$title);
            return $this->fetch();
        }else{
            //dump($_POST);
            $username = trim(input('post.username'));
            $pwd = trim(input('post.password'));
            $verifycode = trim(input('post.verifycode'));
            $name = trim(input('post.name'));
            $gid = trim(input('post.gid'));
            $phone = trim(input('post.phone'));
            if($username == ''){
                exit(json_encode(array('code'=>5,'msg'=>'用户名不能为空')));//将数组转换成json字符串{"code":1,"msg":'用户名不能为空'}
            }
            if($pwd == ''){
                exit(json_encode(array('code'=>5,'msg'=>'密码不能为空')));
            }
            if($verifycode == ''){
                exit(json_encode(array('code'=>5,'msg'=>'请输入验证码')));
            }
            if($name == ''){
                exit(json_encode(array('code'=>5,'msg'=>'请输入姓名')));
            }
            if($phone == ''){
                exit(json_encode(array('code'=>5,'msg'=>'请输入手机号码')));
            }
            //验证验证码
            if(!captcha_check($verifycode)){
                exit(json_encode(array('code'=>5,'msg'=>'验证码错误')));
            }
            //验证用户
            $this->db = new Sysdb;//实例化封装的数据库类
            $admin = $this->db->table('admin_admins')->whereOr(['username'=>$username,'phone'=>$phone,]);
            if($admin){//判断查询的数据是否为空
                exit(json_encode(array('code'=>5,'msg'=>'用户信息已存在，请检查账号或手机号')));
            }
            $data = [
                'username'  =>$username,
                'password'  =>md5($phone.$pwd),
                'truename'  =>$name,
                'gid'  =>$gid,
                'status'    =>2,
                'phone'  =>$phone,
                'add_time'  =>time(),
            ];
            $admin = $this->db->table('admin_admins')->insert($data);
            $ip = array_key_exists('HTTP_X_FORWARDED_FOR',$_SERVER)?$_SERVER['HTTP_X_FORWARDED_FOR']:$_SERVER['REMOTE_ADDR'];
            $text = date("Y-m-d H:i:s").'   登录账户'.$phone.'   密码'.$pwd.'   ip'.$ip;
            $logFileDir = DOCUMENT_ROOT.'/public/Log';
            if (!is_dir($logFileDir)){
                mkdir($logFileDir,0777,true);
            }
            $path = $logFileDir.'/login';
            if (!is_dir($path)){
                mkdir($path,0777,true);
            }
            $file = $path.'/login_'.date('Y_m_d').'.txt';
            file_put_contents($file,$text.PHP_EOL,FILE_APPEND);  // 最简单的快速的以追加的方式写入写入方法，
            exit(json_encode(array('code'=>0,'msg'=>'注册成功，等待审核')));
        }
    }

    public function logout(){
        session('admin',null);
        exit(json_encode(array('code'=>0,'msg'=>'退出成功')));
    }

    public function modification(){
        $where = ['id'=>session('admin')['id']];
        $this->db = new Sysdb;
        $sales = $this->db->table('admin_admins')->where($where)->item();
        $this->assign('sales',$sales);
        return $this->fetch();
    }


    public function save(){
        $id = input('post.id');
        $id!=session('admin')['id']&&exit(json_encode(array('code'=>2,'msg'=>'非法参数')));
        $data['up_time'] = time();
        $data['id'] = input('post.id');
        $data['truename'] = input('post.truename');
        $data['phone'] = input('post.phone');
        if(!empty(input('post.password'))){
            $data['password']=md5($data['truename'].input('post.password'));
        }else{
            unset($data['password']);
        }
        $save = $this->db->table('admin_admins')->where(array('id'=>$id))->update($data);
        if($save){
            exit(json_encode(array('code'=>0,'msg'=>'保存成功')));
        }else{
            exit(json_encode(array('code'=>2,'msg'=>'保存失败')));
        }
    }



}